package com.itany.cloud.config;

import com.itany.cloud.exception.OpenFeignException;
import feign.Logger;
import feign.Response;
import feign.Retryer;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 石小俊
 */
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public Retryer myRetryer(){
        // 参数一:初始间隔时间,单位:毫秒
        // 参数二:重试最大间隔时间,单位:秒
        // 参数三:最大请求次数。其值为重试次数+1(初始访问次数计算1次,重试次数N次)
        // public Default(long period, long maxPeriod, int maxAttempts)
        return new Retryer.Default(100,1,3);
    }

    @Bean
    public ErrorDecoder feignErrorDecoder(){
        return new ErrorDecoder() {
            @Override
            public Exception decode(String methodKey, Response response) {
                return new OpenFeignException(response.status(),"统一异常处理");
            }
        };
    }

}
